package com.javabasic.compare;

import lombok.Data;
//import org.junit.jupiter.api.Test;
import org.junit.Test;


import java.util.*;

public class CompareTest {

    public static void main(String[] args) {
        System.out.println("Hello world");
    }

    @Test
    public void test() {

        List<Student> lists = new ArrayList<>();

        Student stu1 = new Student(1, "Little");

        Student stu2 = new Student(2, "Cyntin");

        Student stu3 = new Student(3, "Tony");

        Student stu4 = new Student(4, "Gemini");

        Student[] stus = new Student[4];

        stus[0] = stu1;

        stus[1] = stu4;

        stus[2] = stu3;

        stus[3] = stu2;

        System.out.println("Array: " + Arrays.toString(stus));

        // Array.sort()  只能排序数组
        Arrays.sort(stus);

        System.out.println("Array Sort With Age: " + Arrays.toString(stus));

        Arrays.sort(stus,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("Array Sort With Name: " + Arrays.toString(stus));

        lists.add(stu2);
        lists.add(stu3);
        lists.add(stu4);
        lists.add(stu1);

        // 排序Collection 以及Map 要用 Collections.sort()方法
        Collections.sort(lists);

        System.out.println("sort lists with age:" + lists.toString());

        Collections.sort(lists, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

        System.out.println("sort lists with name:" + lists.toString());


    }

}
// Student
@Data
class Student implements Comparable<Student>{
    private int age;
    private String name;
//    private String code;

    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int compareTo(Student o) {
        if (age > o.age) {
            return 1;
        } else if (age < o.age) {
            return -1;
        } else {
            return 0;
        }
    }
}
